package org.example.controller;

import dto.MaterialOrderQueryDTO;
import dto.SupplierQueryDTO;
import entity.MaterialOrder;
import entity.Supplier;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.constant.MessageConstant;
import org.example.result.PageResult;
import org.example.result.Result;
import org.example.service.SupervisorService;
import org.springframework.web.bind.annotation.*;
import vo.SupplierListVo;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@Tag(name = "供应链主管控制层")
@RequestMapping("/supervisor")
@RequiredArgsConstructor
public class SupervisorController {

    private final SupervisorService supervisorService;

    // ==================== 供应商管理 ====================

    /**
     * 分页查询供应商
     * 
     * @param pageNumber
     * @param pageSize
     * @param supplierName
     * @return
     */
    @Operation(description = "分页查询供应商")
    @GetMapping("/querySupplier")
    public Result<PageResult> querySupplier(
            @RequestParam(name = "pageNumber") Integer pageNumber,
            @RequestParam(name = "pageSize") Integer pageSize,
            @RequestParam(name = "supplierName", required = false) String supplierName) {
        log.info("分页查询供应商：pageNumber = {}, pageSize = {}, supplierName = {}", pageNumber, pageSize, supplierName);
        SupplierQueryDTO queryDTO = SupplierQueryDTO.builder()
                .pageNumber(pageNumber)
                .pageSize(pageSize)
                .supplierName(supplierName)
                .build();
        PageResult pageResult = supervisorService.querySupplier(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 添加供应商
     * 
     * @param supplier
     * @return
     */
    @Operation(description = "添加供应商")
    @PostMapping("/addSupplier")
    public Result<String> addSupplier(@RequestBody Supplier supplier) {
        log.info("添加供应商：supplier = {}", supplier);
        supervisorService.addSupplier(supplier);
        return Result.success();
    }

    /**
     * 更新供应商
     * 
     * @param supplier
     * @return
     */
    @Operation(description = "更新供应商")
    @PutMapping("/updateSupplier")
    public Result<String> updateSupplier(@RequestBody Supplier supplier) {
        log.info("更新供应商：supplier = {}", supplier);
        supervisorService.updateSupplier(supplier);
        return Result.success(MessageConstant.UPLOAD_SUCCESS);
    }

    /**
     * 删除供应商
     * 
     * @param supplierId
     * @return
     */
    @Operation(description = "删除供应商")
    @DeleteMapping("/deleteSupplier/{supplierId}")
    public Result<String> deleteSupplier(@PathVariable Integer supplierId) {
        log.info("删除供应商：supplierId = {}", supplierId);
        supervisorService.deleteSupplier(supplierId);
        return Result.success();
    }

    /**
     * 查询供应商列表
     * @return
     */
    @Operation(description = "查询供应商列表")
    @GetMapping("/supplierList")
    public Result<List<SupplierListVo>> supplierList() {
        log.info("查询供应商列表");
        List<SupplierListVo> supplierList = supervisorService.getSupplierList();
        return Result.success(supplierList);
    }

    // ==================== 原材料订单管理 ====================

    /**
     * 分页查询原材料订单
     * 
     * @param pageNumber
     * @param pageSize
     * @param orderStatus
     * @param materialName
     * @param createTime
     * @return
     */
    @Operation(description = "分页查询原材料订单")
    @GetMapping("/queryMaterialOrder")
    public Result<PageResult> queryMaterialOrder(
            @RequestParam(name = "pageNumber") Integer pageNumber,
            @RequestParam(name = "pageSize") Integer pageSize,
            @RequestParam(name = "orderStatus", required = false) String orderStatus,
            @RequestParam(name = "materialName", required = false) String materialName,
            @RequestParam(name = "createTime", required = false) LocalDate createTime) {
        log.info("分页查询原材料订单：pageNumber = {}, pageSize = {}, orderStatus = {}, materialName = {}, createTime = {}", 
                pageNumber, pageSize, orderStatus, materialName, createTime);
        MaterialOrderQueryDTO queryDTO = MaterialOrderQueryDTO.builder()
                .pageNumber(pageNumber)
                .pageSize(pageSize)
                .orderStatus(orderStatus)
                .materialName(materialName)
                .createTime(createTime)
                .build();
        PageResult pageResult = supervisorService.queryMaterialOrder(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 添加原材料订单
     * 
     * @param materialOrder
     * @return
     */
    @Operation(description = "添加原材料订单")
    @PostMapping("/addMaterialOrder")
    public Result<String> addMaterialOrder(@RequestBody MaterialOrder materialOrder) {
        log.info("添加原材料订单：materialOrder = {}", materialOrder);
        supervisorService.addMaterialOrder(materialOrder);
        return Result.success();
    }

    /**
     * 更新原材料订单
     * 
     * @param materialOrder
     * @return
     */
    @Operation(description = "更新原材料订单")
    @PutMapping("/updateMaterialOrder")
    public Result<String> updateMaterialOrder(@RequestBody MaterialOrder materialOrder) {
        log.info("更新原材料订单：materialOrder = {}", materialOrder);
        supervisorService.updateMaterialOrder(materialOrder);
        return Result.success(MessageConstant.UPLOAD_SUCCESS);
    }

    /**
     * 删除原材料订单
     * 
     * @param orderId
     * @return
     */
    @Operation(description = "删除原材料订单")
    @DeleteMapping("/deleteMaterialOrder/{orderId}")
    public Result<String> deleteMaterialOrder(@PathVariable Integer orderId) {
        log.info("删除原材料订单：orderId = {}", orderId);
        supervisorService.deleteMaterialOrder(orderId);
        return Result.success();
    }
}